<template>
    <div>
        <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
        <el-row>
            <el-button type="primary">添加角色</el-button>
        </el-row>
        <el-table :data="rolesList" border stripe>
            <el-table-column type="expand">
                <template slot-scope="scope">
                    <el-row v-for="(item,index) in scope.row.children" :key="item.id" class="venter">
                        <!-- 一级权限 -->
                        <el-col :span="5" >
                            <el-tag closable @close="handleRemoveById(scope.row,item.id)" type="danger">{{ item.authName }}</el-tag>
                            <i class="el-icon-caret-right"></i>
                        </el-col>
                        <!-- 二，三级权限 -->
                        <el-col :span="19">
                            <el-row v-for="(item2) in item.children" :key="item2.id" class="venter">
                                <el-col :span="6">
                                    <el-tag  closable @close="handleRemoveById(scope.row,item2.id)" type="success">{{ item2.authName }}</el-tag>
                                    <i class="el-icon-caret-right"></i>
                                </el-col>
                                <el-col :span="18">
                                    <el-tag closable @close="handleRemoveById(scope.row,item3.id)" v-for="(item3) in item2.children" :key="item3.id">{{ item3.authName }}</el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                </template>
            </el-table-column>
            <!-- 索引列 -->
            <el-table-column type="index"></el-table-column>
            <el-table-column label="角色名称" prop="roleName"></el-table-column>
            <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
            <el-table-column label="操作" width="300px">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                    <el-button @click="showAssignmentPermissions(scope.row)" type="warning" icon="el-icon-setting" size="mini">分配权限</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="setAssignmentPermissions" width="50%" @close="handleChangClose">
        <!-- 树形控件 -->
        <el-tree :data="rightsList" :props="treeProps" ref="treeRef" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys"></el-tree>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="allotRights">确 定</el-button>
        </span>
    </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            rolesList:[],
            // 控制对话框的显示与隐藏
            setAssignmentPermissions:false,
            rightsList:[],
            treeProps:{
                label:'authName',
                children:'children'
            },
            defKeys:[],
            roleId:''
        };
    },
    created(){
        this.getRolesList()
    },
    mounted() {
        
    },

    methods: {
        async getRolesList(){
         const {data:res} = await  this.$http.get('roles')
         this.rolesList = res.data
        },
        async handleRemoveById(role,rightId){
            const confirmResult =  await this.$confirm('是否继续删除该权限?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (confirmResult != 'confirm') {
            return this.$message.info('用户取消删除')
        }
        const {data:res} = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        if (res.meta.status !==200) {
            return this.$message.error('删除失败')
          }
          role.children = res.data
        },
        async showAssignmentPermissions(role){
            // 获取所有权限的数据
            this.roleId = role.id
            const {data:res} = await this.$http.get('rights/tree')
            this.rightsList = res.data
            this.getRightKeys(role,this.defKeys)
            this.setAssignmentPermissions = true
            
        },
        // 通过递归的形式获取三级权限的id
        getRightKeys(node,arr){
            // 如果node节点里没有children属性那就是三级节点了， 数据里面没有
            if(!node.children){
                return arr.push(node.id)    
            }
            node.children.forEach(item => {
                this.getRightKeys(item,arr)
            });

        },
        handleChangClose(){
            this.defKeys= []
        },
        // 点击确定为用户提交分配的权限
        async allotRights(){
            const keys=[
                ...this.$refs.treeRef.getCheckedKeys(),
                ...this.$refs.treeRef.getHalfCheckedKeys()
            ]
            const idStr = keys.join(',')
            const {data:res} =await this.$http.post(`roles/${this.roleId}/rights`,{rids:idStr})
            this.$message.success(res.meta.msg)
            this.getRolesList()
            this.setAssignmentPermissions = false
        }
    },
};
</script>

<style lang="less" scoped>

</style>